package com.xy.controller;

import com.xy.base.ApiBaseResponse;
import com.xy.entity.User;
import com.xy.repository.UserRepository;
import com.xy.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Thinkpad on 2018/1/4.
 */
@RestController
@RequestMapping(value = "/open/user")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping(value = "/login")
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<?> login(){
        User user = new User();

        String account = "17681127621";
        String password = "123456";

        if((user = userRepository.findByUsernameAndPassword(account,password)) != null){
            Map<String,Object> m = new HashMap<String,Object>();
            m.put("userid", user.getUuid());
            m.put("userName", user.getUsername());

            String token = JwtUtil.createJavaWebToken(m);

            Map<String, Object> stringObjectMap = JwtUtil.parserJavaWebToken(token);
            return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK, stringObjectMap));
        };

        return null;
    }




}
